﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jinshiyu.Models;

namespace Jinshiyu.Controllers
{
    [Authorize]
    public class PrepaidController : Controller
    {
        private JinshiyuEntities db = new JinshiyuEntities();

        //
        // GET: /Prepaid/

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        //
        // GET: /Prepaid/Details/5

        public ViewResult Details(int id)
        {
            PrepaidRecord 充值记录 = db.PrepaidRecords.Find(id);
            return View(充值记录);
        }

        //
        // GET: /Prepaid/Create

        public ActionResult Create()
        {
            #region 设置充值金额下拉框的SelectListItem
            var priceList = new List<SelectListItem>();
            priceList.Add(new SelectListItem
            {
                Text = "50元",
                Value = "50"
            });
            priceList.Add(new SelectListItem
            {
                Text = "100元",
                Value = "100"
            });
            ViewData["pricelist"] = priceList;
            #endregion

            var cMember = db.CMembers.Single(c => c.账户 == User.Identity.Name);

            PrepaidRecord prepaid = new PrepaidRecord();
            prepaid.会员编号 = cMember.C会员编号;
            prepaid.会员名称 = cMember.姓名;

            return View(prepaid);
        }

        //
        // POST: /Prepaid/Create

        [HttpPost]
        public ActionResult Create(PrepaidRecord record)
        {
            var cMember = db.CMembers.Single(c => c.账户 == User.Identity.Name);
            record.充值账户 = User.Identity.Name;
            record.充值日期 = DateTime.Now;
            record.会员编号 = cMember.C会员编号;
            record.会员名称 = cMember.姓名;

            TempData["record"] = record;

            switch (record.充值类型)
            {
                case "支付宝":
                    return RedirectToAction("Prepaid", "Alipay", record);
                case "充值卡":
                    return RedirectToAction("PrepaidCard");
            }

            return View(record);
        }


        public ActionResult PrepaidCard()
        {
            return View();
        }


        [HttpPost]
        public ActionResult PrepaidCard(PrepaidRecord record)
        {
            var r = (PrepaidRecord)TempData["record"];

            record.充值账户 = r.充值账户;
            record.充值日期 = r.充值日期;
            record.会员编号 = r.会员编号;
            record.会员名称 = r.会员名称;
            record.充值类型 = r.充值类型;

            充值卡 card;
            try
            {
                card = db.PrepaidCards.Single<充值卡>(p => p.充值卡密码 == record.密码);
            }
            catch (Exception e)
            { return RedirectToAction("Fail"); }

            record.卡或交易号 = card.充值卡号;
            record.金额 = card.充值卡面额;
            record.经手人 = card.经手人;
            record.批号 = card.批号;
            record.制作日期 = card.制作日期;

            db.PrepaidRecords.Add(record);

            card.已充值 = true;
            card.充值日期 = record.充值日期;
            card.会员编号 = record.会员编号;
            card.账户 = record.充值账户;

            db.SaveChanges();
            return RedirectToAction("Success");

        }


        public ViewResult Success()
        {
            return View();
        }

        public ViewResult Fail()
        {
            return View();
        }

        //
        // GET: /Prepaid/Edit/5

        public ActionResult Edit(int id)
        {
            PrepaidRecord 充值记录 = db.PrepaidRecords.Find(id);
            return View(充值记录);
        }

        //
        // POST: /Prepaid/Edit/5

        [HttpPost]
        public ActionResult Edit(PrepaidRecord 充值记录)
        {
            if (ModelState.IsValid)
            {
                db.Entry(充值记录).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(充值记录);
        }

        //
        // GET: /Prepaid/Delete/5

        public ActionResult Delete(int id)
        {
            PrepaidRecord 充值记录 = db.PrepaidRecords.Find(id);
            return View(充值记录);
        }

        //
        // POST: /Prepaid/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            PrepaidRecord 充值记录 = db.PrepaidRecords.Find(id);
            db.PrepaidRecords.Remove(充值记录);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult List(PrepaidRecordViewModel orderList)
        {
            ViewBag.Balance = db.CMembers.Single(c => c.账户 == User.Identity.Name).账户余额;

            var orders = db.PrepaidRecords.Where(o => o.充值账户 == User.Identity.Name).OrderByDescending(o => o.充值日期);

            var order = DoIndex(orders, orderList);

            if (Request.IsAjaxRequest())
            {
                return PartialView("PrepaidRecordsGrid", order);
            }
            else
            {
                return View("List", order);
            }
        }

        private PrepaidRecordViewModel DoIndex(IQueryable<PrepaidRecord> orders, PrepaidRecordViewModel orderList)
        {
            orderList.PagerViewModel.TotalItems = orders.Count();

            orderList.ListOfPrepaidRecords = orders
                .Skip(orderList.PagerViewModel.Skip)
                .Take(orderList.PagerViewModel.Take);
            return orderList;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}